﻿#include "HarvestMonster.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    HarvestMonster::HarvestMonster(MonsterInfo *info) : MonsterObject(info)
    {
        RemainingSkinCount = 2;
    }

    void HarvestMonster::Drop()
    {
    }

    bool HarvestMonster::Harvest(PlayerObject *player)
    {
        if (RemainingSkinCount == 0)
        {
            for (int i = _drops.size() - 1; i >= 0; i--)
            {
                if (player->CheckGroupQuestItem(_drops[i]))
                {
                    _drops.erase(_drops.begin() + i);
                }
                else
                {
                    if (player->CanGainItem(_drops[i]))
                    {
                        player->GainItem(_drops[i]);
                        _drops.erase(_drops.begin() + i);
                    }
                }
            }

            if (_drops.empty())
            {
                Harvested = true;
                _drops = std::vector<UserItem*>();
                S::ObjectHarvested *tempVar = new S::ObjectHarvested();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                player->ReceiveChat("You cannot carry anymore.", ChatType::System);
            }

            return true;
        }

        if (--RemainingSkinCount > 0)
        {
            return true;
        }


        _drops = std::vector<UserItem*>();

        for (int i = 0; i < Info->Drops.size(); i++)
        {
            DropInfo *drop = Info->Drops[i];

            int rate = static_cast<int>(drop->Chance / Settings::DropRate);
            if (rate < 1)
            {
                rate = 1;
            }
            if (drop->Gold > 0 || getEnvir()->Random->Next(rate) != 0)
            {
                continue;
            }

            UserItem *item = getEnvir()->CreateDropItem(drop->Item);
            if (item == nullptr)
            {
                continue;
            }

            if (drop->QuestRequired)
            {
                if (!player->CheckGroupQuestItem(item, false))
                {
                    continue;
                }
            }

            if (item->Info->Type == ItemType::Meat)
            {
                item->CurrentDura = static_cast<unsigned short>(std::max(0, item->CurrentDura + Quality));
            }

            _drops.push_back(item);
        }


        if (_drops.empty())
        {
            player->ReceiveChat("Nothing was found.", ChatType::System);
            Harvested = true;
            _drops = std::vector<UserItem*>();
            S::ObjectHarvested *tempVar2 = new S::ObjectHarvested();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            Broadcast(tempVar2);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }


        return true;
    }
}
